package com.tl.web.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tl.app.domain.AppClerk;
import com.tl.app.domain.AppUser;
import com.tl.app.service.IAppClerkService;
import com.tl.app.service.IAppUserService;
import com.tl.common.constant.Constants;
import com.tl.common.core.controller.BaseController;
import com.tl.common.core.domain.AjaxResult;
import com.tl.common.core.domain.entity.SysUser;
import com.tl.common.core.domain.model.LoginBody;
import com.tl.common.utils.SecurityUtils;
import com.tl.framework.web.service.SysLoginService;



/**
 * 外部登录
 * @author 15110
 *
 */
@RestController
@RequestMapping("/api/auth")
public class ApiLoginController extends BaseController{
	
	
	@Autowired
    private IAppUserService appUserService;
	
    @Autowired
    private SysLoginService loginService;
	
	@Autowired
	private IAppClerkService appClerkService;
    
    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/loginWx")
    public AjaxResult loginWx(@RequestBody LoginBody loginBody){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.loginWx(loginBody.getOpenId());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    
    
    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getAppUserInfo")
    public AjaxResult getAppUserInfo(){
    	SysUser user = SecurityUtils.getLoginUser().getUser();
        AppUser au=appUserService.selectAppUserByUserId(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("appUser", au);
        ajax.put("user", user);
        return ajax;
    }
    
    
    /**
     * 获取店员信息
     * 
     * @return 用户信息
     */
    @GetMapping("getClerkInfo")
    public AjaxResult getClerkInfo(){
    	SysUser user = SecurityUtils.getLoginUser().getUser();
    	AppClerk ac=appClerkService.selectAppClerkByUserId(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("clerk", ac);
        ajax.put("user", user);
        return ajax;
    }
    

}
